//
//  ZYNTableViewCellForTableViewCellForNewSong.m
//  VideoPic
//
//  Created by dllo on 16/3/23.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZYNTableViewCellForTableViewCellForNewSong.h"
#import <UIImageView+WebCache.h>
#import "ZYNModelForHotSongs.h"
#import "zynAVPlayerSingleton.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@interface ZYNTableViewCellForTableViewCellForNewSong()
@property (nonatomic, strong) UIImageView *imageForSongPic;
@property (nonatomic, strong) UILabel *labelForSongName;
@property (nonatomic, strong) UILabel *labelForSingerName;
@property (nonatomic, strong) ZYNModelForHotSongs *model;
@property (nonatomic, strong) UILabel *labelForFavoriteCount;

@end
@implementation ZYNTableViewCellForTableViewCellForNewSong



#pragma mark - init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}

#pragma mark - createSubViews
- (void)createSubViews {
    //cell style
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.labelOfCount = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfCount];
    
    self.imageForSongPic = [[UIImageView alloc] init];
    [self.contentView addSubview:self.imageForSongPic];
    self.imageForSongPic.backgroundColor = [UIColor yellowColor];
    
    self.labelForSongName = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForSongName];
    
    self.labelForSingerName = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForSingerName];
    
    self.labelForFavoriteCount = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForFavoriteCount];
    
    self.buttonForPlayOrPause = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonForPlayOrPause];
  
}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.labelOfCount.frame = CGRectMake(10, 10, WIDTH / 10, WIDTH / 10);
    self.labelOfCount.textAlignment = NSTextAlignmentCenter;
    
    self.imageForSongPic.frame = CGRectMake(10 + WIDTH / 10, 5, (HEIGHT - 10), (HEIGHT - 10));
    
    self.labelForSongName.frame = CGRectMake(10 + WIDTH / 10 + (HEIGHT - 10) + 10, 5, WIDTH - WIDTH / 6 - 70,  HEIGHT / 2);
    
    self.labelForSingerName.frame = CGRectMake(10 + WIDTH / 10 + (HEIGHT - 10) + 10, 5 + HEIGHT / 2, [[self class] widthForLabelForSingerName:self.model.singerName], (HEIGHT - 10) / 2);
    
    self.labelForFavoriteCount.frame = CGRectMake([[self class] widthForLabelForSingerName:self.model.singerName] + 10 + WIDTH / 10 + (HEIGHT - 10) + 10 + 5, 5 + HEIGHT / 2, WIDTH / 4, (HEIGHT - 10) / 2);
    
    self.buttonForPlayOrPause.frame = CGRectMake(WIDTH - 50, 10, WIDTH / 10,  (HEIGHT - 20));
    
//    [self.buttonForPlayOrPause setImage:[UIImage imageNamed:@"playgray"] forState:UIControlStateNormal];
    self.buttonForPlayOrPause.selected = YES;
    [self.buttonForPlayOrPause addTarget:self action:@selector(handleClick:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)handleClick:(UIButton *)button {
    NSLog(@"dddddd");
    NSLog(@"%ld", button.tag);
    
    [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[self.model.urlList objectAtIndex:1] objectForKey:@"url"]];
  
}
+ (CGFloat)widthForLabelForSingerName:(NSString *)text {
    CGRect rect = [text boundingRectWithSize:CGSizeMake(0, SCREEN_WIDTH / 14) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil];
    
    return rect.size.width;
}
#pragma mark - deliverModel
- (void)passModel:(ZYNModelForHotSongs *)model {
    self.model = model;
    [self.imageForSongPic sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:[UIImage imageNamed:@"zw"]];
    
    self.labelForSongName.text = model.name;
    self.labelForSongName.font = [UIFont systemFontOfSize:15];
    self.labelForSingerName.text = model.singerName;
    self.labelForSingerName.font = [UIFont systemFontOfSize:14];
    NSString *str = [NSString stringWithFormat:@"%ld", model.favorites];
    if (str.length > 4) {
        CGFloat count = model.favorites;
        
        self.labelForFavoriteCount.text = [NSString stringWithFormat:@"♥️%.f万", count / 10000];
    
    } else {
        self.labelForFavoriteCount.text = [NSString stringWithFormat:@"♥️%ld", model.favorites];
    }
    self.labelForFavoriteCount.font = [UIFont systemFontOfSize:14];

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
